package top.aigoo.project01.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @Description: 根据不同的操作系统获取不同根路径
 * @Author: Mr.OYSHUO.欧阳大叔 mail:1642302522@qq.com
 * @Version 1.0.0
 * @Date: 2021/8/8 22:37
 */

@Configuration

public class PathUtil {
	// 获取当前系统的文件分隔符
	private static String seperator = System.getProperty("file.separator");

	private static String winPath;

	@Value("${win.base.path}")
	public void setWinPath(String winPath) {
		PathUtil.winPath = winPath;
	}

	private static String linuxPath;

	@Value("${linux.base.path}")
	public void setLinuxPath(String linuxPath) {
		PathUtil.linuxPath = linuxPath;
	}

	private static String shopPath;

	@Value("${shop.relevant.path}")
	public void setShopPath(String shopPath) {
		PathUtil.shopPath = shopPath;
	}

	private static String personinfoPath;

	@Value("${personinfo.relevant.path}")
	public void setPersoninfoPath(String personinfoPath) {
		PathUtil.personinfoPath = personinfoPath;
	}

	/**
	 * 为什么不将图片保存在src/main/resource下面？
	 * 因为如果放到src/main/resource下面，那么用户每次上传照片，服务器重启后，这些图片 就找不到了
	 *
	 * @return 存储图片的根路径
	 */
	public static String getImgBasePath() {
		String os = System.getProperty("os.name");
		String basePath = "";
		if (os.toLowerCase().startsWith("win")) {
			// basePath = "G:/baidu/project01_o2o/images/";
			basePath = winPath;
		} else {
			// basePath = "/home/oyshuo/project01_o2o/images/";
			basePath = linuxPath;
		}
		basePath = basePath.replace("/", seperator);
		return basePath;
	}

	/**
	 * 获取店铺图片的存储路径
	 */
	public static String getShopImagePath(long shopId) {
		// 方法1
//		StringBuilder shopImagePathBuilder = new StringBuilder();
//		shopImagePathBuilder.append("/upload/images/item/shop/");	
//		shopImagePathBuilder.append(shopId);
//		shopImagePathBuilder.append("/");
//		String shopImagePath = shopImagePathBuilder.toString().replace("/", seperator);
		String shopImagePath = shopPath + shopId +seperator ;
		shopImagePath = shopImagePath.replace("/", seperator);
		return shopImagePath;
	}

	public static String getPersonInfoImagePath() {

		// String personInfoImagePath = "/upload/images/item/personinfo/";
		String personInfoImagePath = personinfoPath;
		personInfoImagePath = personInfoImagePath.replace("/", seperator);
		return personInfoImagePath;
	}
}
